/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.creation;
import core.messages.client.creation.Msg_Role_Select;
import core.messages.server.creation.Msg_Role_Select_Require;
import core.threads.DataBase;
import core.threads.server.Server_Public_Data;
import core.threads.server.Server_Public_MsgBox;
import core.threads.server.Server_Public_Thread;
import cstAndEnum.CARA;
import java.util.ArrayList;
import java.util.List;
import models.ListPlayer;
import models.Role;

/**
 *
 * @author Invince
 */
public class T_Server_Cara_Selector  extends Thread{
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;

/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        random_Cara();//抽角色，并发武将牌

        Server_Public_MsgBox.refreshScreen();
        Msg_Role_Select_Require msgRoleSelectRequire=new Msg_Role_Select_Require("Server");
        Server_Public_MsgBox.msgBox_Server_Send_Msg.offer(msgRoleSelectRequire);
        /**-----Loop----**/
        while(!this.stop){
            if(!this.suspend){
                int nbRoleChoosen;
                ListPlayer tempListPlayer;
                synchronized(Server_Public_Data.semaNbRoleChoosen){
                    nbRoleChoosen=Server_Public_Data.nbRoleChoosen;
                }
                synchronized(Server_Public_Data.semaTempListPlayer){
                    tempListPlayer=Server_Public_Data.tempListPlayer;
                }
                while(nbRoleChoosen<=tempListPlayer.size()){
                    if(!Server_Public_MsgBox.msgBox_Server_Role_Select.isEmpty()){
                        Msg_Role_Select msgRoleSelect=Server_Public_MsgBox.msgBox_Server_Role_Select.poll();
                        Role roleChoosen=DataBase.findRoleByID(msgRoleSelect.getIdRole());
                        synchronized(Server_Public_Data.semaTempListPlayer){
                            Server_Public_Data.tempListPlayer.findPlayerByName(msgRoleSelect.getSender()).setRole(roleChoosen);
                        }
                    }
                    //Attention here
                    synchronized(Server_Public_Data.semaNbRoleChoosen){
                    nbRoleChoosen=Server_Public_Data.nbRoleChoosen;
                    }
                    synchronized(Server_Public_Data.semaTempListPlayer){
                        tempListPlayer=Server_Public_Data.tempListPlayer;
                    }
                }
                gameCreating();
            }
        }
         /**----End-----**/
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }

    public void gameCreating(){
        ListPlayer tempListPlayer;
        synchronized(Server_Public_Data.semaTempListPlayer){
            tempListPlayer=Server_Public_Data.tempListPlayer;
        }
        synchronized(Server_Public_Data.semaGame){

            Server_Public_Data.game.setListPlayer(tempListPlayer.getList());//注意这里是List<Player>作为参数，不是ListPlayer
            int size=tempListPlayer.size();
            for(int i=0;i<size;i++){
                if(tempListPlayer.get(i).getCara().equals(CARA.MAJESTY)){
                    Server_Public_Data.game.setMajestyID(i);
                    break;
                }
            }

        }
        startGameButtonEnable();
        Server_Public_MsgBox.refreshScreen();
        Server_Public_Thread.tServerNewGame.stopT();
        Server_Public_Thread.tServerCaraNbConfig.stopT();
        Server_Public_Thread.tServerCaraSelect.stopT();
    }

    public void startGameButtonEnable(){
        return;
    }

/*----------Private functions-----------*/
    private void random_Cara(){//随机抽取角色，并发送武将牌
        int nbMajesty;
        int nbLoyal;
        int nbRebel;
        int nbSpy;
        synchronized(Server_Public_Data.semaTempCaraNbConfig){
            nbMajesty=Server_Public_Data.tempCaraNbConfig.getNbMajesty();
            nbLoyal=Server_Public_Data.tempCaraNbConfig.getNbLoyal();
            nbRebel=Server_Public_Data.tempCaraNbConfig.getNbRebel();
            nbSpy=Server_Public_Data.tempCaraNbConfig.getNbSpy();
        }
        List<CARA> tempListCara=new ArrayList<CARA>();
        for(int i=0;i<nbMajesty;i++){
            tempListCara.add(CARA.MAJESTY);
        }
        for(int i=0;i<nbLoyal;i++){
            tempListCara.add(CARA.LOYAL);
        }
        for(int i=0;i<nbRebel;i++){
            tempListCara.add(CARA.REBEL);
        }
        for(int i=0;i<nbSpy;i++){
            tempListCara.add(CARA.SPY);
        }

        synchronized(Server_Public_Data.semaTempListPlayer){
            int size=Server_Public_Data.tempListPlayer.size();
            //Method1:随机打乱列表分配
    //        java.util.Collections.shuffle(tempListCara);//随机打乱list
    //        for(int i=0;i<size;i++){
    //            Server_Public_Data.tempListPlayer.get(i).setCara(tempListCara.get(i));
    //        }
            //Method2:每次抽出来一张，注意概率和上面是不同的
            while(size!=0){
                int indexPlayer=0;//indexPlayer总数==size, 当size==0时，indexPlayer正好到末尾
                int i=(int) Math.round(Math.random()*size);
                Server_Public_Data.tempListPlayer.get(indexPlayer).setCara(tempListCara.remove(i));
                size--;
                indexPlayer++;
            }
        }

        random_Role_Select();//发武将牌
    }

    private void random_Role_Select(){//发武将牌
        synchronized(Server_Public_Data.semaTempListPlayer){
            synchronized(Server_Public_Data.semaListRoleForSelection){
                int size=Server_Public_Data.tempListPlayer.size();
                int totalRole=Server_Public_Data.listRoleForSelection.size();
                for(int i=0;i<size;i++){
                    int nbSelection=3;
                    if(Server_Public_Data.tempListPlayer.get(i).getCara().equals(CARA.MAJESTY)){nbSelection=5;}
                    for(int j=0 ;j<nbSelection;j++){
                        int k=(int) Math.round(Math.random()*totalRole);
                        Role roleAdd=Server_Public_Data.listRoleForSelection.get(k);
                        Server_Public_Data.tempListPlayer.get(i).getRoleSelect().add(roleAdd);
                        Server_Public_Data.listRoleForSelection.remove(k);//把抽出的武将从原来的列表里删掉
                        totalRole--;
                    }
                }
            }
        }
    }
/*----------Constructor-----------------*/
    public T_Server_Cara_Selector() {
        synchronized(this){
            this.stop=false;
            this.suspend=false;
        }
    }
/*----------Gets and sets---------------*/
}
